package com.headwind.logistics.log.Aspect;

import com.headwind.logistics.common.constant.UserSessionKey;
import com.headwind.logistics.common.pojo.Employee;
import com.headwind.logistics.common.util.RedisUtils;
import com.headwind.logistics.log.annotate.Log;
import com.headwind.logistics.log.mapper.LogMapper;
import com.headwind.logistics.log.pojo.FunctionLog;
import com.headwind.logistics.log.util.MySessionContext;
import com.headwind.logistics.permission.mapper.EmployeeMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.function.Function;

/**
 * @Author 小恶魔
 * @Date 2020-07-21 10:24
 * @Version 1.0
 * @DESCRIPTION 功能日志切面
 */
@Aspect
@Component
public class LogAspect {

    @Autowired
    private RedisUtils redisUtils;

    //日志数据访问层
    @Resource
    private LogMapper logMapper;

    @Pointcut("@annotation(com.headwind.logistics.log.annotate.Log)")
    public void logPointCut() {


    }

    /**
     * 切面  注解
     * @param point
     * @return
     * @throws Throwable
     * @throws InterruptedException
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable, InterruptedException {
        //从redis里获取员工id
        Object o = redisUtils.hmGet("EMPLOYEEID", "EMPLOYEE");

        long empId = Long.valueOf(String.valueOf(o)).longValue();

        Object result = point.proceed();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        String name = method.getName();
        Log log = method.getAnnotation(Log.class);

        //创建功能操作
        FunctionLog functionLog = new FunctionLog();
        //员工号
        functionLog.setEmployeeId(empId);
        //注解里面的值
        functionLog.setFunctionName(log.value());

        //调用添加功能操作日志表
        logMapper.addFunctionLog(functionLog);

        return result;
    }

}
